package com.gjm.demo.MessageController;

import lombok.RequiredArgsConstructor;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RequestMapping("/message")
@RestController
public class MessageController {

    //存放消息
    private List<MessageInfo> messageList = new ArrayList<>();

    @PostMapping(value = "/publish1", produces = "application/json")//前端使用json接收
    public String publish1(@RequestBody MessageInfo messageInfo) {//前端使用json传递
//        System.out.println(messageInfo.getSender());
//        System.out.println(messageInfo.getReceiver());
//        System.out.println(messageInfo.getMessage());
        if (!StringUtils.hasLength(messageInfo.getSender()) ||
            !StringUtils.hasLength(messageInfo.getReceiver()) ||
            !StringUtils.hasLength(messageInfo.getMessage())) {
            return "{\"ok\" : 0}";
        }
        messageList.add(messageInfo);
        return "{\"ok\" : 1}";
    }

    @PostMapping(value = "/publish2")//前端使用文本格式接收
    public String publish2(MessageInfo messageInfo) {//前端使用QueryString传递
//        System.out.println(messageInfo.getSender());
//        System.out.println(messageInfo.getReceiver());
//        System.out.println(messageInfo.getMessage());
        if (!StringUtils.hasLength(messageInfo.getSender()) ||
                !StringUtils.hasLength(messageInfo.getReceiver()) ||
                !StringUtils.hasLength(messageInfo.getMessage())) {
            return "{\"ok\" : 0}";
        }
        messageList.add(messageInfo);
        return "{\"ok\" : 1}";
    }

    @PostMapping(value = "/publish3")//前端使用文本格式接收
    public String publish3(@RequestBody MessageInfo messageInfo) {//前端使用json发送
        if (!StringUtils.hasLength(messageInfo.getSender()) ||
                !StringUtils.hasLength(messageInfo.getReceiver()) ||
                !StringUtils.hasLength(messageInfo.getMessage())) {
            return "{\"ok\" : 0}";
        }
        messageList.add(messageInfo);
        return "{\"ok\" : 1}";
    }

    @GetMapping("/getList")
    public List<MessageInfo> getList() {
        return messageList;
    }
}
